//
//  HTStatusToolBarView.swift
//  WeiBoDemo
//
//  Created by 郝涛 on 16/4/21.
//  Copyright © 2016年 郝涛. All rights reserved.
//

import UIKit

class HTStatusToolBarView: UIView {

    var retweetButton:UIButton?
    
    var commentButton:UIButton?
    
    var unlikeButton:UIButton?
    var status : HTStatus? {
        
        didSet{
            
        
            
            let retweetResult = setCountString(status?.reposts_count ?? 0, title: "转发")
            let commentResult = setCountString(status?.comments_count ?? 0, title: "评论")
            let unlikeResult = setCountString(status?.attitudes_count ?? 0, title: "赞")
            
            retweetButton?.setTitle(retweetResult, forState: UIControlState.Normal)
            commentButton?.setTitle(commentResult, forState: UIControlState.Normal)
            unlikeButton?.setTitle(unlikeResult, forState: UIControlState.Normal)
            
        
        }
        
    }
    
    
    //传入文字和转发评论数  返回字符串
    func setCountString(count:Int,title:String)->String{
        
        if count <= 0{
            return title
        }else if count > 0 && count < 10000{
            return "\(count)"
        }else{
            // 转成 CGFloat
            let num = CGFloat(count / 1000) / 10
            // Int 转字符串
            var countString = "\(num)万"
            
            // 判断我们的字符串中'.0'
            
            if countString.containsString(".0"){
                countString = countString.stringByReplacingOccurrencesOfString(".0万", withString: "万")
            }
            
            return countString
    
    }
    }
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置视图
    private func setupUI(){
        self.backgroundColor = UIColor.whiteColor()
        //添加控件
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButton("timeline_icon_unlike", title: "赞")
        let line1 = addChildlineImage()
        let line2 = addChildlineImage()
        
        //设置约束
        retweetButton!.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        commentButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        unlikeButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        line1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(retweetButton!.snp_centerY)
        }
        line2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(commentButton!.snp_centerY)
        }
        
    }
    //添加按钮方法
    func addChildButton(imgName:String,title:String)->UIButton{
        
        let button = UIButton()
        
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        addSubview(button)
        return button
    }
    //创建丨线
    func addChildlineImage()->UIImageView{
        let lmg = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(lmg)
        return lmg
    }
    
    
}
